package com.shadow.demo.generator;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 代码自动生成
 * @author shadow
 */
public final class CodeGenerator {

    /**
     *  数据库连接信息
     *  如果你的MySQL版本为8，必需要在数据库地址后面加上时区, 像serverTimezone=Asia/Shanghai这个
     */
    private static final String DB_URL = "jdbc:mysql://121.40.111.51:3306/info?serverTimezone=GMT%2B8&useUnicode=true"
            + "&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true";
    private static final String DB_USER = "root";
    private static final String DB_PWD = "dist@2026";
    private static final String PARENT_PATH = CodeGenerator.class.getPackage().getName().replace(".generator","");
    //2、生成代码的存放路径
    private static final String OUTPUT_DIR = System.getProperty("user.dir") + "/app/generator/src/main/java";
    //3、生成代码对应的表名
    private static final List<String> TABLES = new ArrayList<>(Collections.singletonList("ip_info"));
    //4、过滤表明前缀，类名根据表名生成
    private static final String TABLE_PER_FIX = "";
    //5、类文件的创建作者
    private static final String AUTHOR = "shadow";

    public static void main(String[] args) {
        FastAutoGenerator.create(DB_URL, DB_USER, DB_PWD)
                //全局配置
                .globalConfig(builder -> builder.author(AUTHOR)  //作者
                        .outputDir(OUTPUT_DIR)  //输出路径
                        .fileOverride()  //开启覆盖已生成文件
                            .disableOpenDir()  //禁止打开输出目录
//                            .enableSwagger()  //开启swagger
//                            .dateType(DateType.ONLY_DATE) //时间策略
                        .commentDate("yyyy-MM-dd") //注释日期格式
                )
                //包配置
                .packageConfig(builder -> builder.parent(PARENT_PATH) //父包名
                       .moduleName("") //父包模块名
                       .entity("entity") //entity路径
                       .controller("controller") //controller路径
                       .service("service") //service路径
                       .serviceImpl("service.impl") //serviceImpl路径
                       .mapper("mapper") //mapper路径
                       .xml("mapper") //mapper xml路径
                       .pathInfo(Collections.singletonMap(OutputFile.mapperXml, OUTPUT_DIR + "/resources"))) //路径配置信息
                //策略配置
                .strategyConfig(builder -> builder.addInclude(TABLES) //表名
                        .addTablePrefix(TABLE_PER_FIX) //过滤表名前缀
                        .entityBuilder() //entity 策略配置
                        .enableLombok() //开启lombok
                        .logicDeleteColumnName("deleted") //说明逻辑删除是哪个字段
                        .enableTableFieldAnnotation() //属性加上说明注释
                        .controllerBuilder() //controller 策略配置
                        .formatFileName("%sController") //%s适配,根据表名替换,生成controller名
                        .enableRestStyle() //开启 RestController
                        .serviceBuilder() //service 策略配置
                        .formatServiceFileName("%sService") //service名
                        .formatServiceImplFileName("%sServiceImpl") //serviceImpl名
                        .mapperBuilder() //mapper 策略配置
                        .enableBaseResultMap() //生成通用的resultMap
                        .superClass(BaseMapper.class) //继承的父类
                        .formatMapperFileName("%sMapper") //mapper名
                        .enableMapperAnnotation() //@mapper开启
                        .formatXmlFileName("%sMapper")) //xml名
                //模板配置
                .templateConfig(builder -> builder.entity("templates/entity.java"))
                //模板引擎配置,使用 Freemarker 模板引擎,默认是 velocity 模板引擎
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}